package com.hptpd.questionnairesurvey.controller.wx;

import com.google.common.collect.Maps;
import com.hptpd.questionnairesurvey.component.Result;
import com.hptpd.questionnairesurvey.controller.base.BaseController;
import com.hptpd.questionnairesurvey.service.ISurveyService;
import com.hptpd.questionnairesurvey.vo.UserSurveyVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019/11/4
 * \*
 * \* Description:
 * \
 *
 * @author walter_long
 */
@RestController
@RequestMapping("/api/wx")
public class WxSurveyController extends BaseController {
    @Resource(name = "iSurveyService")
    private ISurveyService iSurveyService;

    /**
     * 微信小程序获取openid
     * @param userId String
     * @return Result
     */
    @RequestMapping(value = "/survey/count", method = RequestMethod.GET)
    public Result countByUserId(
            @RequestParam(defaultValue = "") String userId){
        Map<String, Object> paramMap = Maps.newLinkedHashMap();
        paramMap.put("userId", userId);
        return iSurveyService.countByUserId(paramMap);
    }

    /**
     * 列出调查
     * @param userId String
     * @param isDone String
     * @param page Integer
     * @param limit Integer
     * @return Result
     */
    @RequestMapping(value = "/survey/list", method = RequestMethod.GET)
    public Result listSurveysByPage(
            @RequestParam String userId,
            @RequestParam(defaultValue = "") Boolean isDone,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        Pageable pageable = PageRequest.of(page - 1, limit);
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userId", userId);
        paramMap.put("isDone", isDone);
        return iSurveyService.listWxSurveysByPage(paramMap, pageable);
    }

    /**
     * 用户完成调查
     * @param userSurveyVo UserSurveyVo
     * @return Result
     */
    @RequestMapping(value = "/user/survey/done", method = RequestMethod.POST)
    public Result userDoneSurvey(
            @RequestBody UserSurveyVo userSurveyVo){
        return iSurveyService.userDoneSurvey(userSurveyVo);
    }

    /**
     * 用户调查详情
     * @param userSurveyId String
     * @return Result
     */
    @RequestMapping(value = "/user/survey/detail", method = RequestMethod.GET)
    public Result getUserSurvey(
            @RequestParam(defaultValue = "") String userSurveyId){
        return iSurveyService.getUserSurvey(userSurveyId);
    }

    /**
     * 用户完成调查
     * @param userSurveyVo UserSurveyVo
     * @return Result
     */
    @RequestMapping(value = "/user/survey/grades", method = RequestMethod.POST)
    public Result getCalculateSurveyResultGrades(
            @RequestBody UserSurveyVo userSurveyVo){
        return iSurveyService.getCalculateSurveyResultGrades(userSurveyVo);
    }

}
